package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.CommVLayoutAdapter;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.ui.fragment.TempFragment;
import com.chexiehui.jc.widget.LoadingLayout;
import com.chexiehui.jc.widget.NonSlidingGridView;
import com.flyco.tablayout.SlidingTabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 差异配置
 */
public class CarBrandDifferentConfigActivity extends BaseActivity {
    @BindView(R.id.vp_tepm_page)
    ViewPager vpTepmPage;
    @BindView(R.id.stl_car_year_tab)
    SlidingTabLayout stlCarYearTab;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;

    private DelegateAdapter listAdapter;
    private CommVLayoutAdapter<String> imageListAdapter;

    private String[] titles = new String[]{"2020款", "2019款", "停售"};
    private ArrayList<Fragment> forumFragmentList = new ArrayList<>();

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("差异配置")
                .setDividerVisible(false);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_car_brand_different_config;
    }

    @Override
    protected void initView(Bundle var1) {
        loadingLayout.setStatus(LoadingLayout.Success);
        forumFragmentList = new ArrayList<>();
        vpTepmPage.setOffscreenPageLimit(titles.length);
        forumFragmentList.add(TempFragment.getInstance());
        forumFragmentList.add(TempFragment.getInstance());
        forumFragmentList.add(TempFragment.getInstance());
        stlCarYearTab.setViewPager(vpTepmPage, titles, this, forumFragmentList);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableOverScrollDrag(true);
        refreshLayout.setDragRate(0.5f);

        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(mContext);
        mRecycler.setLayoutManager(virtualLayoutManager);
        ((SimpleItemAnimator) mRecycler.getItemAnimator()).setSupportsChangeAnimations(false);
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        mRecycler.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        listAdapter = new DelegateAdapter(virtualLayoutManager, false);
        mRecycler.setAdapter(listAdapter);

        int dp_10 = SizeUtils.dp2px(10);
        LinearLayoutHelper helper = new LinearLayoutHelper(SizeUtils.dp2px(10));
        helper.setMargin(0,0, 0, dp_10);
        helper.setBgColor(ContextCompat.getColor(mContext, R.color.colorTransparent));
        imageListAdapter = new CommVLayoutAdapter<String>(mContext, new ArrayList<>(),
                R.layout.item_car_brand_different_config, helper) {
            @Override
            public void convert(ViewHolder holder, int position, String item) {
                NonSlidingGridView nsgv_item_image = holder.getView(R.id.nsgv_item_image);
                List<String> images = new ArrayList<>();
                images.add("");
                images.add("");
                images.add("");
                images.add("");
                images.add("");
                images.add("");
                nsgv_item_image.setAdapter(new CommonAdapter<String>(mContext, R.layout.item_car_brand_different_config_image, images) {
                    @Override
                    protected void convert(com.zhy.adapter.abslistview.ViewHolder viewHolder, String item, int position) {
                        ImageView iv_item_image = viewHolder.getView(R.id.iv_item_image);
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv_item_image.getLayoutParams();
                        lp.width = BigDecimal.valueOf(ScreenUtils.getScreenWidth())
                                .subtract(BigDecimal.valueOf(SizeUtils.dp2px(30)))
                                .divide(BigDecimal.valueOf(2), 2, BigDecimal.ROUND_HALF_UP).intValue();
                        lp.height = BigDecimal.valueOf(lp.width).multiply(BigDecimal.valueOf(57))
                                .divide(BigDecimal.valueOf(82), 2, BigDecimal.ROUND_HALF_UP).intValue();
                        iv_item_image.setLayoutParams(lp);
                    }
                });
            }
        };
        listAdapter.addAdapter(imageListAdapter);
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.getData().add("");
        imageListAdapter.notifyDataSetChanged();
        
    }
}
